package com.example.wishstar.repository;

import android.util.Log;

import com.example.wishstar.api.Api;
import com.example.wishstar.utils.ApiResponse;
import com.google.gson.JsonObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ResetRepository {
    private String TAG = this.getClass().getName();
    private Api api;

    public ResetRepository(Api api) {
        this.api = api;
    }

    public interface ResetCallBack {
        // 获取验证码的callback
        void callback_getCode(Response<ApiResponse<String>> response);
        // 重置的callback
        void callback_reset(Response<ApiResponse<String>> response);
    }

    public void getCode(int state, String userMail, ResetRepository.ResetCallBack resetCallBack) {
        api.getCode(2, userMail).enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                if(response.body() != null) {
                    Log.e(TAG, "发送验证码接口调用返回值" + response.body().getCode().toString());
                    resetCallBack.callback_getCode(response);
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                Log.e(TAG, "发送验证码接口调用失败");

            }
        });
    }

    public void reset(String code, String password, String userMail, ResetRepository.ResetCallBack resetCallBack) {
        JsonObject fieldMap = new JsonObject();
        fieldMap.addProperty("code", code);
        fieldMap.addProperty("password", password);
        fieldMap.addProperty("userMail", userMail);

        api.reset(RequestBody.create(MediaType.parse("application/json"), fieldMap.toString())).enqueue(new Callback<ApiResponse<String>>() {
            @Override
            public void onResponse(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                if(response.body() != null) {
                    Log.e(TAG, "重置密码接口调用返回值" + response.body().getCode().toString());
                    resetCallBack.callback_reset(response);
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                Log.e(TAG, "重置密码接口调用失败");
            }
        });
    }
}
